module.exports = function ($scope, info, $stateParams, slist, devicetype, $resource, update, remove, what,FileUploader) {

	//机器id
	var id = $stateParams.id;

	$scope.updateFlag = true;
	$scope.what = what;

	$scope.lock = true;

	//机器类型
  $scope.typearr = devicetype;
  
  $scope.pay_arr = [] // 支付方式
	$scope.payarr = [
    {
        'label': '现金',
        'value1': '1',
        'checked': false
    },
    {
        'label': '微信',
        'value1': '2',
        'checked': false
    },
    {
        'label': '支付宝',
        'value1': '3',
        'checked': false
    },
    {
        'label': '龙支付',
        'value1': '4',
        'checked': false
    }
  ];
  $scope.login_info = []

	//景区下拉
	slist({'type': 'D'}).then(function (res){
		if (res.errcode === 0) {
			$scope.viewarr = res.data;
		} else {
			alert(res.errmsg);
		}
	});

	//模板下拉
	$scope.search = function (pageNo){
        var para = { 
            'pageNo': pageNo || $scope.currentPage,
            'pageSize':'1000'
        }
        $resource('/api/as/tc/ticketStyle/findList', {}, {}).save(para, function(res){
            if(res.errcode === 0){
                $scope.viewlist = res.data.results;
            }else{
                toaster.error({title: '', body: res.errmsg});
            }
		});
    }
	$scope.search();
	//票机详情
	info.get({ 'id': id }, function (res) {
		if (res.errcode === 0) {
			$scope.obj = res.data;
      console.log($scope.obj.pay_type,'支付方式');
      if ($scope.obj.pay_type) {
        $scope.pay_arr = $scope.obj.pay_type.split(',')
      }
      for (var i = 0; i < $scope.payarr.length; i++) {
        for (var j = 0; j < $scope.pay_arr.length; j++) {
          if ($scope.pay_arr[j] == $scope.payarr[i].value1) {
            $scope.payarr[i].value = true 
          }
        }
      }
      $scope.obj.pass_word = getCaption($scope.obj.seller_login_code, 1)
      console.log($scope.obj.pass_word, '密码')
      $scope.obj.login_code = getCaption($scope.obj.seller_login_code, 0)
			$scope.obj.id = id;
		}
		else {
			alert(res.errmsg);
		}

	});

	function trim(str) {
		return str.replace(/(^\s+)|(\s+$)/g, "");
	}
  function getCaption(obj, state) {
    var index = obj.lastIndexOf("\,");
    if(state == 0){
    obj = obj.substring(0, index);
    } else {
    obj = obj.substring(index+1, obj.length);
    }
    return obj;
  }

	// 图片
	var uploader2 = $scope.uploader2 = new FileUploader({
        url: 'https://txy.juyouhx.com/Api/Api/ObjectToOss?topdir=ticket&selfdir=device'+Date.parse(new Date())
    });

    uploader2.filters.push({
        name: 'imageFilter',
        fn: function(item /*{File|FileLikeObject}*/, options) {
            var type = '|' + item.type.slice(item.type.lastIndexOf('/') + 1) + '|';
            return '|jpg|png|jpeg|bmp|gif|'.indexOf(type) !== -1;
        }
	});
	
	uploader2.onSuccessItem = function(fileItem, response, status, headers) {   
        $scope.obj.img = response.savename;
    };


	$scope.gogo = function () {
    $scope.pay_arr = []
    for (var i = 0; i < $scope.payarr.length; i++) {
      if ($scope.payarr[i].value == true) {
        $scope.pay_arr.push($scope.payarr[i].value1)
      }
    }
    if ($scope.pay_arr) {
      $scope.obj.pay_type = $scope.pay_arr.join(',')
    }
		if ($scope.obj.type == 5 || $scope.obj.type == 6) {
			if ($scope.obj.login_code == undefined || trim($scope.obj.login_code) == '') {
				alert('请填写登录用户')
				return;
      }
      if ($scope.obj.pass_word == undefined || trim($scope.obj.pass_word) == '') {
				alert('请填写登录密码')
				return;
      }
      $scope.obj.seller_login_code = $scope.obj.login_code + ',' + $scope.obj.pass_word
		} else {
			delete $scope.obj.print_ticket_length;
			delete $scope.obj.print_ticket_width;
			delete $scope.obj.print_ticket_x_axis;
			delete $scope.obj.print_ticket_y_axis;
		}
		console.log($scope.obj.img)
		update.save($scope.obj, function (res) {

			if (res.errcode !== 0) {
				alert(res.errmsg);
			}
			else {
				alert('修改成功');
			}

		});

	};

	$scope.remove = function () {

		remove.save({ 'device_code': $scope.obj.code }, function (res) {

			if (res.errcode === 0 || res.errcode === 1105) {
				$scope.lock = false;
			}
			else {
				alert(res.errmsg);
			}

		});
	}

};